package test;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

public class HelloJsoup {
    private static final String CITY_SPACE = "      ";
    private static final String AREA_SPACE = "            ";
    public static void main(String[] args) throws Exception {
       getRegionSql();
    }

    /**
     * 行政区划代码若为非 “00” 结尾，可将区划级别视为 三级；
     * 行政区划代码若为 “00” 结尾，可将区划级别视为 二级；
     * 行政区划代码若为 “0000” 结尾，可将区划级别视为 一级；
     * @throws Exception
     */
    public static  void getRegionSql () throws Exception {
        String url = "http://www.mca.gov.cn/article/sj/xzqh/2020/20201201.html";
        int count = 0;

        Document doc = Jsoup.connect(url)
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0 Win64 x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36")
                .header("Accept", "text/html,application/xhtml+xml,application/xmlq=0.9,image/webp,image/apng,*/*q=0.8,application/signed-exchangev=b3")
                .maxBodySize(0)
                .timeout(100000)
                .get();

        Elements trs = doc.select("tr");

        for (Element tr : trs ) {
            Elements tds = tr.select("td");

            if (tds.size() > 3) {
                String regionCode = tds.get(1).text();
                String regionArea = tds.get(2).text();
                String parentCode = "";

//                if (validCode(regionCode)) {
                if (regionCode.length()>3) {
                    int leveType = 2;

                    parentCode = regionCode.substring(0,2) + "0000";

                    if (!regionCode.endsWith("00")) {
                        leveType = 3;
                        parentCode = regionCode.substring(0,4) + "00";
                    }

                    if (regionCode.endsWith("0000")) {
                        leveType = 1;
                        parentCode = "000000";
                    }

                    count++;
                    //按照规则生成SQl语句
                    String content = String.format("insert into region_code (code, name, level, parent_code, dtime, note, ctime)" +
                            " values (%s, '%s', %s, %s, '201903', '系统生成', NOW());" + System.getProperty("line.separator"), regionCode, regionArea, leveType, parentCode);
                    System.out.println(content);
                }
            }
        }

        System.out.println("总数量为：" + count);
    }
}
